/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.Candidate;
import entities.CandidateSearchInterest;
import entities.Search;
import entities.SearchEducationalInfoRequerimentAndOperator;
import entities.SearchEducationalInfoRequerimentOrOperator;
import entities.SearchProfessionalInfoRequerimentAndOperator;
import entities.SearchProfessionalInfoRequerimentOrOperator;
import facades.CandidateFacade;
import facades.CandidateSearchInterestFacade;
import facades.SearchFacade;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import sqlgenerators.search.SearchSQLGenerator;
import sqlgenerators.search.SearchSqlGeneratorInterface;
import util.EncryptTool;

/**
 *
 * @author Alvaro
 */
@Named
@ApplicationScoped
public class JobOffer implements Serializable {
   
   private String offerName;
   private String encryptedCandidateId;
   private String searchId;
   private Boolean allOk;
   @Inject
   private CandidateFacade candidateFacade;
   @Inject
   private SearchFacade searchFacade;
   private SearchSqlGeneratorInterface sqlGenerator;
   @Inject
   private CandidateSearchInterestFacade candidateSearchInterestFacade;
   private Search search;
   private Boolean dataNull;
   private Boolean candidateValidInSearch;
   private Boolean existCandidateSearchInterest;
   
   public JobOffer(){
       sqlGenerator=new SearchSQLGenerator();
   }

   public void check()throws Exception {
        allOk=false;
        candidateValidInSearch=true;
        dataNull=true;
        existCandidateSearchInterest=false;
       if(encryptedCandidateId!=null && searchId!=null){
           EncryptTool encrypTool = new EncryptTool ();
           Integer idCandidate;
           Candidate candidate;
           try{
                search=searchFacade.find(new Integer(searchId));
                idCandidate=new Integer(encrypTool.decrypt(encryptedCandidateId.replace("-", "/")));
                candidate=candidateFacade.find(idCandidate);
           }
           catch(Exception e){
               candidate=null;
               search=null;
           }

           
           if(candidate!=null && getSearch()!=null){
               dataNull=false;
               String sql=sqlGenerator.getSqlFromSearch(getSearch());
                candidateValidInSearch= searchFacade.candidateInASearch(candidate, sql);
                existCandidateSearchInterest= candidateSearchInterestFacade.exitsCandidateSearchInterest(candidate, getSearch());
               if(getCandidateValidInSearch() && !getExistCandidateSearchInterest()){
                        CandidateSearchInterest candidateSearchInterest= new CandidateSearchInterest();
                        candidateSearchInterest.setCandidate(candidate);
                        candidateSearchInterest.setSearch(getSearch());
                        candidateSearchInterest.setDate(new Date());
                        candidateSearchInterestFacade.create(candidateSearchInterest);
                        allOk=true;
                }   
            }
        }
    }
   
   public List<Integer> getAllSectorOfSelectedSearch(){
        List<Integer>sectorIds=new ArrayList<Integer>();
        if(search!=null){
            if(search.getProfessionalInfoSector()!=null){
                sectorIds.add(search.getProfessionalInfoSector().getIdSector());
                if(search.getProfessionalInfoSector().getSearchEducationalInfoRequerimentAndOperatorList()!=null){
                    for(SearchEducationalInfoRequerimentAndOperator sector: search.getProfessionalInfoSector().getSearchEducationalInfoRequerimentAndOperatorList()){
                        if(sector.getSector()!=null && !sectorIds.contains(sector.getSector().getIdSector())){
                            sectorIds.add(sector.getSector().getIdSector());
                        }
                    }
                }
                if(search.getProfessionalInfoSector().getSearchEducationalInfoRequerimentOrOperatorList()!=null){
                    for(SearchEducationalInfoRequerimentOrOperator sector: search.getProfessionalInfoSector().getSearchEducationalInfoRequerimentOrOperatorList()){
                        if(sector.getSector()!=null && !sectorIds.contains(sector.getSector().getIdSector())){
                            sectorIds.add(sector.getSector().getIdSector());
                        }
                    }
                }
                if(search.getProfessionalInfoSector().getSearchProfessionalInfoRequerimentAndOperatorList()!=null){
                    for(SearchProfessionalInfoRequerimentAndOperator sector: search.getProfessionalInfoSector().getSearchProfessionalInfoRequerimentAndOperatorList()){
                          if(sector.getSector()!=null && !sectorIds.contains(sector.getSector().getIdSector())){
                              sectorIds.add(sector.getSector().getIdSector());
                          }
                    }  
                }
                if(search.getProfessionalInfoSector().getSearchProfessionalInfoRequerimentOrOperatorList()!=null){
                    for(SearchProfessionalInfoRequerimentOrOperator sector: search.getProfessionalInfoSector().getSearchProfessionalInfoRequerimentOrOperatorList()){
                        if(sector.getSector()!=null && !sectorIds.contains(sector.getSector().getIdSector())){
                            sectorIds.add(sector.getSector().getIdSector());
                        }
                    }
                }
            }
        }
        return sectorIds;
    }
   
   public String goIndex(){
       return "/?faces-redirect=true";
   }
    /**
     * @return the offerName
     */
    public String getOfferName(){
        return offerName;
    }

    /**
     * @param offerName the offerName to set
     */
    public void setOfferName(String offerName) {
        this.offerName = offerName;
    }

    /**
     * @return the encryptedCandidateId
     */
    public String getEncryptedCandidateId() {
        return encryptedCandidateId;
    }

    /**
     * @param encryptedCandidateId the encryptedCandidateId to set
     */
    public void setEncryptedCandidateId(String encryptedCandidateId) {
        this.encryptedCandidateId = encryptedCandidateId;
    }

    /**
     * @return the searchIdString
     */
    public String getSearchId() {
        return searchId;
    }

    /**
     * @param searchIdString the searchIdString to set
     */
    public void setSearchId(String searchIdString) {
        this.searchId = searchIdString;
    }

    /**
     * @return the validFields
     */
    public Boolean getAllOk() {
        return allOk;
    }

    /**
     * @param allOk the allOk to set
     */
    public void setAllOk(Boolean allOk) {
        this.allOk = allOk;
    }

    /**
     * @return the search
     */
    public Search getSearch() {
        return search;
    }

    /**
     * @param search the search to set
     */
    public void setSearch(Search search) {
        this.search = search;
    }

    /**
     * @return the dataNull
     */
    public Boolean getDataNull() {
        return dataNull;
    }

    /**
     * @param dataNull the dataNull to set
     */
    public void setDataNull(Boolean dataNull) {
        this.dataNull = dataNull;
    }

    /**
     * @return the candidateValidInSearch
     */
    public Boolean getCandidateValidInSearch() {
        return candidateValidInSearch;
    }

    /**
     * @param candidateValidInSearch the candidateValidInSearch to set
     */
    public void setCandidateValidInSearch(Boolean candidateValidInSearch) {
        this.candidateValidInSearch = candidateValidInSearch;
    }

    /**
     * @return the existCandidateSearchInterest
     */
    public Boolean getExistCandidateSearchInterest() {
        return existCandidateSearchInterest;
    }

    /**
     * @param existCandidateSearchInterest the existCandidateSearchInterest to set
     */
    public void setExistCandidateSearchInterest(Boolean existCandidateSearchInterest) {
        this.existCandidateSearchInterest = existCandidateSearchInterest;
    }
}
